package com.umeox.watch.moto.gallery.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.umeox.watch.moto.gallery.R;
import com.umeox.watch.moto.gallery.event.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class MakeFrameAdapter extends RecyclerView.Adapter {
    private List<Integer> mResIdlist = new ArrayList<>();
    private String mImagePath;
    private OnItemClickListener mItemClickListener;


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_make_frame, viewGroup, false);

        return new FrameViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        final FrameViewHolder frameViewHolder = (FrameViewHolder) viewHolder;
        final int position = i;
        frameViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                frameViewHolder.mDoneIv.setVisibility(View.VISIBLE);

                if (mItemClickListener != null) {
                    mItemClickListener.onClick(v, position);
                }

            }
        });
        frameViewHolder.bindData(mResIdlist.get(i));
    }

    @Override
    public int getItemCount() {
        return mResIdlist.size();
    }


    public void setDatalist(List<Integer> datalist) {
        mResIdlist.clear();
        mResIdlist.addAll(datalist);
        notifyDataSetChanged();
    }

    public void setImagePath(String imagePath) {
        mImagePath = imagePath;
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    class FrameViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIconIv;
        private ImageView mIconFgIv;
        private ImageView mDoneIv;

        public FrameViewHolder(@NonNull View itemView) {
            super(itemView);
            mIconIv = itemView.findViewById(R.id.iv_item_frame);
            mIconFgIv = itemView.findViewById(R.id.iv_item_fg);
            mDoneIv = itemView.findViewById(R.id.iv_item_done);
        }


        public void bindData(int resId) {
            final Context context = mIconIv.getContext();
            Glide.with(context).load(mImagePath).into(mIconIv);
            mIconFgIv.setImageResource(resId);
        }
    }


}
